﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DataLib.DBUtility;


namespace DataLib.DAL
{
    /// <summary>
    /// 数据访问类:Cumtomer
    /// </summary>
    public partial class Cumtomer
    {
        public Cumtomer()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("id", "Cumtomer");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int? id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Cumtomer");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DataLib.Model.Cumtomer model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Cumtomer(");
            strSql.Append("companyname,shortname,customercode,State,province,city,settlement,postcode,linkman,linkman2,phone,phone2,Email,Email2,BankName,BankAccount,BankCode,PutOnRecord,BankAddress,SaleMan)");
            strSql.Append(" values (");
            strSql.Append("@companyname,@shortname,@customercode,@State,@province,@city,@settlement,@postcode,@linkman,@linkman2,@phone,@phone2,@Email,@Email2,@BankName,@BankAccount,@BankCode,@PutOnRecord,@BankAddress,@SaleMan)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@companyname", SqlDbType.NVarChar,200),
                    new SqlParameter("@shortname", SqlDbType.NVarChar,50),
					new SqlParameter("@customercode", SqlDbType.NVarChar,30),
					new SqlParameter("@State", SqlDbType.NVarChar,30),
					new SqlParameter("@province", SqlDbType.NVarChar,30),
					new SqlParameter("@city", SqlDbType.NVarChar,30),
					new SqlParameter("@settlement", SqlDbType.NVarChar,100),
					new SqlParameter("@postcode", SqlDbType.NVarChar,12),
					new SqlParameter("@linkman", SqlDbType.NVarChar,16),
					new SqlParameter("@linkman2", SqlDbType.NVarChar,16),
					new SqlParameter("@phone", SqlDbType.NVarChar,20),
					new SqlParameter("@phone2", SqlDbType.NVarChar,20),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Email2", SqlDbType.NVarChar,50),
					new SqlParameter("@BankName", SqlDbType.NVarChar,50),
					new SqlParameter("@BankAccount", SqlDbType.NVarChar,50),
					new SqlParameter("@BankCode", SqlDbType.NVarChar,50),
					new SqlParameter("@PutOnRecord", SqlDbType.NVarChar,30),
					new SqlParameter("@BankAddress", SqlDbType.NVarChar,50),
					new SqlParameter("@SaleMan", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.companyname;
            parameters[1].Value = model.shortname;
            parameters[2].Value = model.customercode;
            parameters[3].Value = model.State;
            parameters[4].Value = model.province;
            parameters[5].Value = model.city;
            parameters[6].Value = model.settlement;
            parameters[7].Value = model.postcode;
            parameters[8].Value = model.linkman;
            parameters[9].Value = model.linkman2;
            parameters[10].Value = model.phone;
            parameters[11].Value = model.phone2;
            parameters[12].Value = model.Email;
            parameters[13].Value = model.Email2;
            parameters[14].Value = model.BankName;
            parameters[15].Value = model.BankAccount;
            parameters[16].Value = model.BankCode;
            parameters[17].Value = model.PutOnRecord;
            parameters[18].Value = model.BankAddress;
            parameters[19].Value = model.SaleMan;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DataLib.Model.Cumtomer model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Cumtomer set ");
            strSql.Append("companyname=@companyname,");
            strSql.Append("shortname=@shortname,");
            strSql.Append("customercode=@customercode,");
            strSql.Append("State=@State,");
            strSql.Append("province=@province,");
            strSql.Append("city=@city,");
            strSql.Append("settlement=@settlement,");
            strSql.Append("postcode=@postcode,");
            strSql.Append("linkman=@linkman,");
            strSql.Append("linkman2=@linkman2,");
            strSql.Append("phone=@phone,");
            strSql.Append("phone2=@phone2,");
            strSql.Append("Email=@Email,");
            strSql.Append("Email2=@Email2,");
            strSql.Append("BankName=@BankName,");
            strSql.Append("BankAccount=@BankAccount,");
            strSql.Append("BankCode=@BankCode,");
            strSql.Append("PutOnRecord=@PutOnRecord,");
            strSql.Append("BankAddress=@BankAddress,");
            strSql.Append("SaleMan=@SaleMan");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@companyname", SqlDbType.NVarChar,200),
                    new SqlParameter("@shortname", SqlDbType.NVarChar,50),
					new SqlParameter("@customercode", SqlDbType.NVarChar,30),
					new SqlParameter("@State", SqlDbType.NVarChar,30),
					new SqlParameter("@province", SqlDbType.NVarChar,30),
					new SqlParameter("@city", SqlDbType.NVarChar,30),
					new SqlParameter("@settlement", SqlDbType.NVarChar,100),
					new SqlParameter("@postcode", SqlDbType.NVarChar,12),
					new SqlParameter("@linkman", SqlDbType.NVarChar,16),
					new SqlParameter("@linkman2", SqlDbType.NVarChar,16),
					new SqlParameter("@phone", SqlDbType.NVarChar,20),
					new SqlParameter("@phone2", SqlDbType.NVarChar,20),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Email2", SqlDbType.NVarChar,50),
					new SqlParameter("@BankName", SqlDbType.NVarChar,50),
					new SqlParameter("@BankAccount", SqlDbType.NVarChar,50),
					new SqlParameter("@BankCode", SqlDbType.NVarChar,50),
					new SqlParameter("@PutOnRecord", SqlDbType.NVarChar,30),
					new SqlParameter("@BankAddress", SqlDbType.NVarChar,50),
					new SqlParameter("@SaleMan", SqlDbType.NVarChar,50),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.companyname;
            parameters[1].Value = model.shortname;
            parameters[2].Value = model.customercode;
            parameters[3].Value = model.State;
            parameters[4].Value = model.province;
            parameters[5].Value = model.city;
            parameters[6].Value = model.settlement;
            parameters[7].Value = model.postcode;
            parameters[8].Value = model.linkman;
            parameters[9].Value = model.linkman2;
            parameters[10].Value = model.phone;
            parameters[11].Value = model.phone2;
            parameters[12].Value = model.Email;
            parameters[13].Value = model.Email2;
            parameters[14].Value = model.BankName;
            parameters[15].Value = model.BankAccount;
            parameters[16].Value = model.BankCode;
            parameters[17].Value = model.PutOnRecord;
            parameters[18].Value = model.BankAddress;
            parameters[19].Value = model.SaleMan;
            parameters[20].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int? id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Cumtomer ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Cumtomer ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataLib.Model.Cumtomer GetModel(int? id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,companyname,shortname,customercode,State,province,city,settlement,postcode,linkman,linkman2,phone,phone2,Email,Email2,BankName,BankAccount,BankCode,PutOnRecord,BankAddress,SaleMan from Cumtomer ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            DataLib.Model.Cumtomer model = new DataLib.Model.Cumtomer();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataLib.Model.Cumtomer DataRowToModel(DataRow row)
        {
            DataLib.Model.Cumtomer model = new DataLib.Model.Cumtomer();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["companyname"] != null)
                {
                    model.companyname = row["companyname"].ToString();
                }
                if (row["shortname"] != null)
                {
                    model.shortname = row["shortname"].ToString();
                }
                if (row["customercode"] != null)
                {
                    model.customercode = row["customercode"].ToString();
                }
                if (row["State"] != null)
                {
                    model.State = row["State"].ToString();
                }
                if (row["province"] != null)
                {
                    model.province = row["province"].ToString();
                }
                if (row["city"] != null)
                {
                    model.city = row["city"].ToString();
                }
                if (row["settlement"] != null)
                {
                    model.settlement = row["settlement"].ToString();
                }
                if (row["postcode"] != null)
                {
                    model.postcode = row["postcode"].ToString();
                }
                if (row["linkman"] != null)
                {
                    model.linkman = row["linkman"].ToString();
                }
                if (row["linkman2"] != null)
                {
                    model.linkman2 = row["linkman2"].ToString();
                }
                if (row["phone"] != null)
                {
                    model.phone = row["phone"].ToString();
                }
                if (row["phone2"] != null)
                {
                    model.phone2 = row["phone2"].ToString();
                }
                if (row["Email"] != null)
                {
                    model.Email = row["Email"].ToString();
                }
                if (row["Email2"] != null)
                {
                    model.Email2 = row["Email2"].ToString();
                }
                if (row["BankName"] != null)
                {
                    model.BankName = row["BankName"].ToString();
                }
                if (row["BankAccount"] != null)
                {
                    model.BankAccount = row["BankAccount"].ToString();
                }
                if (row["BankCode"] != null)
                {
                    model.BankCode = row["BankCode"].ToString();
                }
                if (row["PutOnRecord"] != null)
                {
                    model.PutOnRecord = row["PutOnRecord"].ToString();
                }
                if (row["BankAddress"] != null)
                {
                    model.BankAddress = row["BankAddress"].ToString();
                }
                if (row["SaleMan"] != null)
                {
                    model.SaleMan = row["SaleMan"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,companyname,shortname,customercode,State,province,city,settlement,postcode,linkman,linkman2,phone,phone2,Email,Email2,BankName,BankAccount,BankCode,PutOnRecord,BankAddress,SaleMan ");
            strSql.Append(" FROM Cumtomer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,companyname,shortname,customercode,State,province,city,settlement,postcode,linkman,linkman2,phone,phone2,Email,Email2,BankName,BankAccount,BankCode,PutOnRecord,BankAddress,SaleMan ");
            strSql.Append(" FROM Cumtomer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Cumtomer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from Cumtomer T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Cumtomer";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod
        /// <summary>
        /// 获取分公司下面的客户
        /// </summary>
        /// <param name="compcity"></param>
        /// <returns></returns>
        public DataTable GetDataByCity(string compcity)
        {
            DataTable dt = new DataTable();

            SqlParameter[] sqlparameter ={
                new SqlParameter("@compcity",SqlDbType.VarChar,20)
        };
            sqlparameter[0].Value = compcity;
            SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_GetCustomerByCity", sqlparameter);
            dt.Load(dr);
            dr.Close();
            return dt;
        }

        /// <summary>
        /// 检查客户编码是否存在
        /// </summary>
        /// <param name="cumcode"></param>
        /// <returns></returns>
        public bool CodeIsExits(string cumcode,int? id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Cumtomer");
            strSql.Append(" where customercode=@customercode and id<>@id");
            SqlParameter[] parameters = {
					new SqlParameter("@customercode", SqlDbType.VarChar,20),
                    new SqlParameter("@id",SqlDbType.Int)
			};
            parameters[0].Value = cumcode;
            parameters[1].Value = id;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        #endregion  ExtensionMethod
    }
}

